package com.happok.onlineclass.usermanagement.setting.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.happok.onlineclass.usermanagement.common.exception.DisableException;
import com.happok.onlineclass.usermanagement.common.exception.MisdirectedRequestException;
import org.springframework.util.ObjectUtils;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/10/12 11:05
 */
public class SmsUtil {
    public static void connectToAliSms(String accessKeyId, String accessKeySecret, String domain) {
        if (ObjectUtils.isEmpty(accessKeyId) ||
                ObjectUtils.isEmpty(accessKeySecret) ||
                ObjectUtils.isEmpty(domain)) {
            throw new DisableException("短信平台设置异常");
        }
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysVersion("2017-05-25");
        request.setSysAction("QuerySmsTemplate");
        request.setSysDomain(domain);
        request.putQueryParameter("TemplateCode", "测试模板");
        try {
            client.getCommonResponse(request);
        } catch (Exception e) {
            throw new MisdirectedRequestException(2, "短信服务器连接异常");
        }
    }

    public static void sendSmsVerifyCode(String accessKeyId, String accessKeySecret, String domain, String signName,
                                         String phone, Integer verifyCode, String templateCode) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = createCommonRequest(domain, phone, signName, templateCode);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", verifyCode);
        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());
        client.getCommonResponse(request);
    }

    public static void sendSmsResetPwd(String accessKeyId, String accessKeySecret, String domain, String signName,
                                         String phone, String pwd, String templateCode) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = createCommonRequest(domain, phone, signName, templateCode);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("password", pwd);
        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());
        client.getCommonResponse(request);
    }

    public static void sendSmsAddUser(String accessKeyId, String accessKeySecret, String domain, String signName,
                                       String phone, String account, String pwd, String templateCode) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = createCommonRequest(domain, phone, signName, templateCode);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("account", account);
        jsonObject.put("password", pwd);
        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());
        client.getCommonResponse(request);
    }

    private static CommonRequest createCommonRequest(String domain, String phone, String signName, String templateCode){
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(domain);
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        return request;
    }
}